//
//  ZPRegisterLoginViewController.m
//  ZPIOSEducation
//
//  Created by mini1 on 2016/11/30.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPRegisterLoginViewController.h"
#import "ZPRegisterVerificationViewController.h"
#import "ZPChangePwdViewController.h"
#import "ZPBaseTabBarController.h"
#import "ZPBaseNavigationController.h"
#import "ZPRegisterInformationViewController.h"

@interface ZPRegisterLoginViewController () <UITextFieldDelegate>
{
    MBProgressHUD *hud;
    NSInteger      flags;
}
// 清除电话号码Btn
//@property (weak, nonatomic) IBOutlet UIButton   *clearPhoneNumBtn;
// 可视登录密码Btn
@property (weak, nonatomic) IBOutlet UIButton   *visibilityPasswordBtn;
// 忘记密码Btn
@property (weak, nonatomic) IBOutlet UIButton   *forgetPasswordBtn;
// 登录Btn
@property (weak, nonatomic) IBOutlet UIButton   *loginBut;
// App图片
@property (weak, nonatomic) IBOutlet UIImageView *AppImgView;
// 电话号TF
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
// 密码TF
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation ZPRegisterLoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.loginBut.enabled = NO;
    self.phoneNumberTextField.delegate = self;
    self.passwordTextField.delegate = self;
    self.passwordTextField.secureTextEntry = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
    
    // 获取最后一次登录的账号
    NSString *lastStr =[zUserDefaults objectForKey:LastLogInfomation] ;
    if (lastStr) {
        self.phoneNumberTextField.text = [NSString stringWithFormat:@"%@",lastStr];
    }
    // hud
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    [hud showAnimated:YES];
    hud.hidden = YES;
    hud.label.text = @"发送中...";
    [self.view addSubview:hud];
    
    flags = 100;
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.view.backgroundColor = zMainColor;
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange) name:UITextFieldTextDidChangeNotification object:nil];
}



- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}


#pragma mark - <键盘隐藏>
- (void)dismissKeyboard
{
    [self.phoneNumberTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}


#pragma mark - <登录按钮是否可点击>
- (void)textFieldDidChange
{
    if (self.phoneNumberTextField.text.length != 0 && self.passwordTextField.text.length != 0) {
        self.loginBut.enabled = YES;
        [self.loginBut setBackgroundColor:zFreeColor(255, 86, 86, 1.0)];
    } else {
        self.loginBut.enabled = NO;
        [self.loginBut setBackgroundColor:zFreeColor(255, 255, 255, 0.4)];
    }
}


#pragma mark - <密码是否可见>
- (IBAction)isShowPwdButtonClick:(id)sender
{
    UIButton *tempBtn = (UIButton *)sender;
    tempBtn.selected = !tempBtn.selected;
    self.passwordTextField.secureTextEntry = !tempBtn.selected;
}


#pragma mark - <登录>
- (IBAction)loginWithType:(id)sender
{
    [self loginWithUserName:self.phoneNumberTextField.text andPass:self.passwordTextField.text];
}


- (void) loginWithUserName:(NSString *)userName andPass:(NSString *)password
{
    [self showHudithString:@"正在登录..."];
    NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:
                               userName,@"mobile",
                               [password zp_md5String],@"password",
                               [NSString  stringWithFormat:@"%@",[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"deviceID"]],@"idfv", nil];
    NSLog(@"=======%@",paramDic);
    [[ZPNetworking  sharedZPNetworking] POST:APIPPT@"access/onLoginByTeacher.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"=========%@",responseObject);
        [self  hiddenHud];
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserInfo.plist"];
            NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
            if (!dataDic) {
                dataDic = [[NSMutableDictionary  alloc] init];
            }
            NSDictionary  *resultDic = [responseObject  objectForKey:@"result"];
            [dataDic  setObject:[NSString  stringWithFormat:@"%@",userName] forKey:@"mobile"];
            [dataDic  setObject:[NSString  stringWithFormat:@"%@",password] forKey:@"passwd"];
            
            NSString  *teacherId = [resultDic  objectForKey:@"teacherId"];
            if (teacherId && ![teacherId  isEqual:[NSNull  null]]) {
                [dataDic  setObject:[NSString  stringWithFormat:@"%@",teacherId] forKey:@"memberId"];
            }
            NSString  *access_token = [resultDic  objectForKey:@"access_token"];
            if (access_token && ![access_token  isEqual:[NSNull  null]]) {
                [dataDic  setObject:[NSString  stringWithFormat:@"%@",access_token] forKey:@"token"];
            }
            NSString  *headUrl = [resultDic  objectForKey:@"headUrl"];
            if (headUrl && ![headUrl  isEqual:[NSNull  null]]) {
                [dataDic  setObject:[NSString  stringWithFormat:@"%@",headUrl] forKey:@"headUrl"];
            }
            NSString  *teacherName = [resultDic  objectForKey:@"teacherName"];
            if (teacherName && ![teacherName  isEqual:[NSNull  null]]) {
                [dataDic  setObject:[NSString  stringWithFormat:@"%@",teacherName] forKey:@"userName"];
            }
            
            [dataDic  writeToFile:homePath atomically:YES];
            
            [[ZPAuthStore sharedZPAuthStore] resetUserInfoDictionary];
            [[ZPAuthStore sharedZPAuthStore] setWebViewCookiesWithType:1];
            [[NSNotificationCenter  defaultCenter] postNotificationName:@"logOnNotification" object:nil];
            [zUserDefaults setObject:[NSString stringWithFormat:@"%@",userName] forKey:LastLogInfomation];
            self.view.window.rootViewController = [[ZPBaseTabBarController alloc] init];
        }else{
            [ZPPromptView showMessage:[responseObject objectForKey:@"codeMsg"]];
        }
    } failure:^(NSError * _Nonnull error) {
        [self  hiddenHud];
        [ZPPromptView showMessage:@"登录失败"];

        
    }];
}

#pragma mark - <点击忘记密码>
- (IBAction)forgetPwdButtonClick:(id)sender
{
    ZPChangePwdViewController *changerPwdVc = [[ZPChangePwdViewController alloc] init];
    changerPwdVc.flag = flags;
//    [self presentViewController:[[ZPBaseNavigationController alloc] initWithRootViewController: changerPwdVc]  animated:YES completion:nil];
    [self.navigationController pushViewController:changerPwdVc animated:YES];
//    [self.navigationController setNavigationBarHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
